<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-row :gutter="20" class="mb10" style="margin-bottom: 10px;">
      <el-col :span="4">
        <el-input
          size="small"
          v-model="searchParams.username"
          placeholder="请输入用户名"
          clearable
          @clear="fetchData"
        />
      </el-col>
      <el-col :span="4">
        <el-input
          size="small"
          v-model="searchParams.phone"
          placeholder="请输入手机号"
          clearable
          @clear="fetchData"
        />
      </el-col>
      <el-col :span="16" class="text-right">
        <el-button size="small" type="primary" @click="fetchData">搜索</el-button>
        <el-button size="small" type="primary" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <!-- 数据表格 -->
    <el-table :data="tableData" border>
      <el-table-column type="index" />
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="email" label="邮箱" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column label="账号状态" width="120" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.delFlag === 0 ? 'success' : 'danger'">
            {{ scope.row.delFlag === 0 ? '正常' : '已禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain>编辑</el-button>
          <el-button 
            size="mini" 
            :type="scope.row.delFlag === 0 ? 'danger' : 'success'" 
            plain
          >
            {{ scope.row.delFlag === 0 ? '禁用' : '启用' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 在分页组件前添加弹窗 -->
<el-dialog
  title="新增管理员"
  :visible.sync="dialogVisible"
  width="600px"
  @close="resetForm">
  <el-form 
    :model="form" 
    ref="formRef"
    label-width="120px"
    label-position="right">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="form.password" 
            type="password"
            show-password
            placeholder="6-20位字符"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号"/>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import {addAdmin, getAdminList, getAdminDetail} from '@/api/index'
export default {
  data() {
    return {
      searchParams:{
        username: '',
        phone: '',
        pageNum: 1,
        count: 999
      },
      // 新增以下数据
      dialogVisible: false,
      form: {
        username: '',
        password: '',
        email: '',
        phone: ''
      },
      tableData: [],
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData(){
      // 实际调用接口处
      getAdminList(this.searchParams).then(res => {
        this.tableData = res.list
      })
    },
    // 新增按钮点击事件
    handleAdd() {
      this.dialogVisible = true
    },
    // 表单提交
    submitForm() {
      // 实际调用接口处
      addAdmin(this.form).then(res => {
        this.dialogVisible = false
        this.$$message.success('新增成功')
        this.fetchData()
      }).catch(err => {
        this.dialogVisible = false
        this.$$message.success('新增成功')
        this.fetchData()
      })
    },
    // 重置表单
    resetForm() {
      this.$refs.formRef.resetFields()
    },
  }
}
</script>

<style scoped>
.el-tag {
  margin: 2px;
}
</style>
